package com.atguigu.yygh.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

@Configuration
public class CorsConfig {

    //处理跨域问题的全局过滤器
    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();

        config.addAllowedMethod("*");//对所有请求方式都支持跨域处理，get  post  put  ...
        config.addAllowedOrigin("*");//允许所有的源，允许客户端的任意的域
        config.addAllowedHeader("*");//对所有的请求头都支持跨域处理

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config); //针对所有路径的请求，都按照上述原则支持跨域
        return new CorsWebFilter(source);
    }
}